from django.shortcuts import render
from django import forms
from apps.app01 import models
from django.core.validators import RegexValidator
from django.core import validators


# Create your views here.

class RegisterModelForm(forms.ModelForm):
    mobile_phone = forms.CharField(label='手机号', validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误'), ],
                                   widget=forms.TextInput(attrs={'class': 'form-control'}))
    password = forms.CharField(label='密码', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
    confirm_password = forms.CharField(label='确认密码', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
    code = forms.CharField(label='验证码', widget=forms.TextInput(attrs={'class': 'form-control'}))

    class Meta:
        model = models.UserInfo  # 指定模型类
        fields = '__all__'
        widgets = {
            'username': forms.TextInput(attrs={'class': 'form-control'}),
            'email': forms.EmailInput(attrs={'class': 'form-control'}),
        }

    # 重写init方法可以批量设置样式
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)  # 继承父类
        for name, field in self.fields.items():
            field.widget.attrs = {'class': 'form-control', 'placeholder': f'请输入{field.label}'}


def register(request):
    form = RegisterModelForm()
    return render(request, 'user/register.html', {'form': form})
